package com.czmh.etc.onlinepubsys.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czmh.etc.onlinepubsys.dto.CmsQueryContentParam;
import com.czmh.etc.onlinepubsys.json.cmg.JSONResult;
import com.czmh.etc.onlinepubsys.model.CmsContent;
import com.czmh.etc.onlinepubsys.services.CmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资讯信息表 前端控制器
 * </p>
 *
 * @author eric
 * @since 2021-06-07
 */
@Api(tags = "资讯管理-公告", description = "资讯管理")
@RestController
@RequestMapping("/cms/cmsContent")
public class CmsContentController {

    @Autowired
    CmsContentService cmsContentService;


    @ApiOperation("根据分类id获取资讯列表-cmsContentCategoryId：0默认，1首页背景图 2首页视频")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult queryList(@RequestBody CmsQueryContentParam cmsQueryContentParam) {
        JSONResult result = new JSONResult();
        IPage<CmsContent> cmsContentCategories = cmsContentService.queryList(cmsQueryContentParam.getCategoryId(), cmsQueryContentParam.getPageSize(), cmsQueryContentParam.getPageNum());
        result.setRspData(JSON.toJSONString(cmsContentCategories));
        return result;
    }

    @ApiOperation("根据ID获取资讯详情")
    @RequestMapping(value = "info/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult info(@PathVariable Long id) {
        JSONResult result = new JSONResult();
        CmsContent cmsContent = cmsContentService.getById(id);
        cmsContent.setReadCount(cmsContent.getReadCount() + 1);
        cmsContentService.updateById(cmsContent);
        result.setRspData(JSON.toJSONString(cmsContent));
        return result;
    }

}

